//
//  FCChangePinVC.m
//  FundChain
//
//  Created by 肖浩然的mac on 2018/3/5.
//  Copyright © 2018年 RUIXIN. All rights reserved.
//

#import "FCChangePasswordVC.h"
#import "FCLoginVC.h"
@interface FCChangePasswordVC ()<UITextFieldDelegate>{
    UIView     *_viewNav;
}

/* 旧密码 */
@property (strong, nonatomic) UITextField *oldTextfield;
/* 新密码 */
@property (strong, nonatomic) UITextField *newfield;
/* 确定密码 */
@property (strong, nonatomic) UITextField *confirmTextfield;
/* 完成按钮 */
@property (strong, nonatomic)  UIButton *completeBtn;


//3个提示Label
/* <#Annotation#> */
@property (strong, nonatomic) UILabel *tipsLabel1;
@property (strong, nonatomic) UILabel *tipsLabel2;
@property (strong, nonatomic) UILabel *tipsLabel3;
@end

@implementation FCChangePasswordVC

//-(void)viewDidDisappear:(BOOL)animated{
//    [super viewDidDisappear:animated];
//        self.navigationController.navigationBarHidden = NO;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    [FCLocalUserHelper saveIsMainVC:@"0"];
    self.view.backgroundColor = kWhiteColor;
    [self setNav];
    [self setupUI];
}
#pragma mark-setupUI
-(void)setupUI{
    //old pin
    UILabel *oldPasswordLabel = [UILabel new];
    [self.view addSubview:oldPasswordLabel];
    [oldPasswordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_viewNav.mas_bottom).mas_offset(60*KScreenHeightScale1334);
        make.left.mas_equalTo(30*KScreenWidthScale750);
    }];
    oldPasswordLabel.textColor = kGrayColor;
    oldPasswordLabel.text = Localized(@"旧密码");
    oldPasswordLabel.font = Kfont(32);
    //textfield
    UITextField *oldPasswordField = [UITextField new];
    self.oldTextfield = oldPasswordField;
    [self.view addSubview:oldPasswordField];
    [oldPasswordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(oldPasswordLabel);
        make.top.mas_equalTo(oldPasswordLabel.mas_bottom).offset(40*KScreenHeightScale1334);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
    }];
    oldPasswordField.font = Kfont(36);
    oldPasswordField.secureTextEntry = YES;
    oldPasswordField.delegate = self;
    //分割线
    UIView *viewLine =[UIView new];
    [self.view addSubview:viewLine];
    [viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(oldPasswordField);
        make.top.mas_equalTo(oldPasswordField.mas_bottom);
        make.height.mas_equalTo(1.0);
    }];
    viewLine.backgroundColor =kLineColor;
    //提示label1
    UILabel *tipsLabel = [UILabel new];
    self.tipsLabel1 = tipsLabel;
    [self.view addSubview:tipsLabel];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewLine.mas_bottom).mas_offset(5*KScreenHeightScale1334);
        make.left.mas_equalTo(viewLine);
    }];
    tipsLabel.text = Localized(@"密码不能为空");
    tipsLabel.textColor = kwarningColor;
    tipsLabel.font = Kfont(24);
    tipsLabel.hidden = YES;
    //newPin
    UILabel *newPasswordLabel = [UILabel new];
    [self.view addSubview:newPasswordLabel];
    [newPasswordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewLine.mas_bottom).mas_offset(80*KScreenHeightScale1334);
        make.left.mas_equalTo(30*KScreenWidthScale750);
    }];
    newPasswordLabel.textColor = kGrayColor;
    newPasswordLabel.text = Localized(@"新密码");
    newPasswordLabel.font = Kfont(32);
    //textfield
    UITextField *newTextfield = [UITextField new];
    self.newfield = newTextfield;
    [self.view addSubview:newTextfield];
    [newTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(newPasswordLabel);
        make.top.mas_equalTo(newPasswordLabel.mas_bottom).offset(40*KScreenHeightScale1334);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
    }];
    newTextfield.font = Kfont(36);
    newTextfield.secureTextEntry = YES;
    newTextfield.delegate = self;
    //分割线
    UIView *viewLine1 =[UIView new];
    [self.view addSubview:viewLine1];
    [viewLine1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(newTextfield);
        make.top.mas_equalTo(newTextfield.mas_bottom);
        make.height.mas_equalTo(1.0);
    }];
    viewLine1.backgroundColor =kLineColor;
    //提示label2
    UILabel *tipsLabel2 = [UILabel new];
    self.tipsLabel2 = tipsLabel2;
    [self.view addSubview:tipsLabel2];
    [tipsLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewLine1.mas_bottom).mas_offset(5*KScreenHeightScale1334);
        make.left.mas_equalTo(viewLine1);
    }];
    tipsLabel2.text = Localized(@"输入6-12位新密码");
    tipsLabel2.textColor = kwarningColor;
    tipsLabel2.font = Kfont(24);
    tipsLabel2.hidden = YES;
    //confirmNewPin
    UILabel *confirmnewPasswordLabel = [UILabel new];
    [self.view addSubview:confirmnewPasswordLabel];
    [confirmnewPasswordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewLine1.mas_bottom).mas_offset(80*KScreenHeightScale1334);
        make.left.mas_equalTo(30*KScreenWidthScale750);
    }];
    confirmnewPasswordLabel.textColor = kGrayColor;
    confirmnewPasswordLabel.text = Localized(@"确认新密码");
    confirmnewPasswordLabel.font = Kfont(32);
    //textfield
    UITextField *confirmTextfield = [UITextField new];
    self.confirmTextfield = confirmTextfield;
    [self.view addSubview:confirmTextfield];
    [confirmTextfield mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(confirmnewPasswordLabel);
        make.top.mas_equalTo(confirmnewPasswordLabel.mas_bottom).offset(40*KScreenHeightScale1334);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
    }];
    confirmTextfield.font = Kfont(36);
    confirmTextfield.secureTextEntry = YES;
    confirmTextfield.delegate = self;
    [confirmTextfield addTarget:self action:@selector(textFieldDidEndChange:) forControlEvents:(UIControlEventEditingDidEnd)];
    //分割线
    UIView *viewLine2 =[UIView new];
    [self.view addSubview:viewLine2];
    [viewLine2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(confirmTextfield);
        make.top.mas_equalTo(confirmTextfield.mas_bottom);
        make.height.mas_equalTo(1.0);
    }];
    viewLine2.backgroundColor =kLineColor;
    //提示label3
    UILabel *tipsLabel3 = [UILabel new];
    self.tipsLabel3 = tipsLabel3;
    [self.view addSubview:tipsLabel3];
    [tipsLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(viewLine2.mas_bottom).mas_offset(5*KScreenHeightScale1334);
        make.left.mas_equalTo(viewLine2);
    }];
    tipsLabel3.text = Localized(@"输入6-12位新密码");
    tipsLabel3.textColor = kwarningColor;
    tipsLabel3.font = Kfont(24);
    tipsLabel3.hidden = YES;
    //complete
    UIButton *completeBtn = [UIButton new];
    self.completeBtn = completeBtn;
    [self.view addSubview:completeBtn];
    [completeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30*KScreenWidthScale750);
        make.right.mas_equalTo(-30*KScreenWidthScale750);
        make.bottom.mas_equalTo(-100*KScreenHeightScale1334);
        make.height.mas_equalTo(110*KScreenHeightScale1334);
    }];
    completeBtn.layer.cornerRadius = 5.0;
    completeBtn.layer.masksToBounds = YES;
    completeBtn.titleLabel.font = KOverstrikingfont(36);
    [completeBtn setTitle:Localized(@"完成") forState:(UIControlStateNormal)];
    [completeBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [completeBtn setBackgroundColor:kLightblueColor];
    //事件
    [completeBtn addTarget:self action:@selector(completeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [oldPasswordField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [newTextfield addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [confirmTextfield addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
#pragma mark-textfieldDelegate

-(void)textFieldDidChange:(UITextField *)textField{
    
    
    if (self.oldTextfield == textField) {
        if (textField.text.trimWhitespace.length > 0) {
            self.tipsLabel1.hidden = YES;
            
        }else{
            self.tipsLabel1.hidden = NO;
        }
        
    }
    if (self.newfield == textField) {
        if (textField.text.trimWhitespace.length > 0) {
            self.tipsLabel2.hidden = YES;
            
        }else{
            self.tipsLabel2.hidden = NO;
        }
        
    }
    if (self.confirmTextfield == textField) {
        if (textField.text.trimWhitespace.length > 0) {
            self.tipsLabel3.hidden = YES;
            
        }else{
            self.tipsLabel3.hidden = NO;
        }
        
    }
    if(self.oldTextfield == textField ||self.newfield == textField ||self.confirmTextfield == textField){
        if (textField.text.trimWhitespace.length > 16) {
            textField.text = [textField.text substringToIndex:16];
            
        }
    }
}
-(void)textFieldDidEndChange:(UITextField *)textField{
    
    
    if(self.confirmTextfield == textField ){
        if (self.confirmTextfield.text.trimWhitespace.length > 0 && self.oldTextfield.text.trimWhitespace.length > 0&&self.newfield.text.trimWhitespace.length > 0) {
            
            [self.completeBtn setBackgroundColor:kRedOrangeColor];
        }
    }
}
#pragma mark-修改密码
-(void)completeBtnClick{
    
    
    if (self.oldTextfield.text.trimWhitespace.length <= 0) {
        self.tipsLabel1.hidden = NO;
        return;
    }
    if (self.newfield.text.trimWhitespace.length <= 0) {
        self.tipsLabel2.hidden = NO;
        return;
    }
    if (self.confirmTextfield.text.trimWhitespace.length <= 0 ) {
        
        self.tipsLabel3.hidden = NO;
        return;
        
    }
    if (![self.newfield.text.trimWhitespace isEqualToString: self.confirmTextfield.text.trimWhitespace]) {
        self.tipsLabel3.text = Localized(@"新密码和确认密码不一致");
        self.tipsLabel3.hidden = NO;
        return;
    }
    if ([self.oldTextfield.text.trimWhitespace isEqualToString:self.newfield.text.trimWhitespace]&&[self.oldTextfield.text.trimWhitespace isEqualToString:self.confirmTextfield.text.trimWhitespace]) {
        self.tipsLabel2.text = Localized(@"新密码不能和旧密码一致");
        self.tipsLabel2.hidden = NO;
        return;
    }
    [self showChrysanthemumAlert:nil];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    [dict setObject:self.oldTextfield.text.trimWhitespace forKey:@"oldPassWord"];
    [dict setObject:self.newfield.text.trimWhitespace forKey:@"newPassWord"];
    NSArray *arrM =[FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",arrM[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",arrM[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    NSString *xidStr = [FCLocalUserHelper getXidStr];
    [dict setObject:xidStr forKey:@"XID"];
    NSString *url = Update_password_URL;
    
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            NSLog(@"%@",responseObject);
            if ([responseObject[@"code"] integerValue] == 200) {
                [self hideProgress];
//                [self showAlert:Localized(@"修改密码成功")];
                //返回登录界面
                [self.navigationController popViewControllerAnimated:YES];
                
            }else if ([responseObject[@"code"] integerValue] == 406){
                [self hideProgress];
                self.tipsLabel1.text = Localized(@"原密码填写错误");
                self.tipsLabel1.hidden = NO;
            }else if ([responseObject[@"code"] integerValue] == 407){
                [self hideProgress];
                self.tipsLabel2.text = Localized(@"新密码格式不正确");
                self.tipsLabel2.hidden = NO;
            }
            
        }
        
    } faile:^(NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        }
        
    }];
    
    
}

#pragma mark-设置导航栏
-(void)setNav{
    self.navigationController.navigationBarHidden = YES;
    _viewNav =[UIView new];
    [self.view addSubview:_viewNav];
    [_viewNav mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                
                make.left.right.top.mas_equalTo(self.view.safeAreaInsets);
                make.height.mas_equalTo(88);
            }else{
                make.left.right.mas_equalTo(0);
                make.height.mas_equalTo(64);
                make.top.mas_equalTo(0);
            }
        }else{
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(64);
            make.top.mas_equalTo(0);
        }
        
    }];
    _viewNav.backgroundColor = kRedOrangeColor;
    
    UIButton *leftBtn = [UIButton new];
    [self.view addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15*KScreenWidthScale750);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
   [leftBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    //返回
    UILabel *backLabel = [UILabel new];
    
    [_viewNav addSubview:backLabel];
    [backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_viewNav);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
    }];
    backLabel.text = Localized(@"更改密码");
    backLabel.textColor = [UIColor whiteColor];
    backLabel.font = KOverstrikingfont(36);
    
    //事件
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)leftBtnClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //移除第一响应
    [self.oldTextfield resignFirstResponder];
    [self.newfield resignFirstResponder];
    [self.confirmTextfield resignFirstResponder];
}
@end

